package com.egao.common.manage.company;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.company.entity.vo.PositionFormVO;
import com.egao.common.module.company.entity.vo.PositionListVO;
import com.egao.common.module.company.service.PositionService;
import com.egao.common.module.push.constants.PushConstants;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.entity.Major;
import com.egao.common.module.system.service.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 单位职位管理
 * @author dl
 */
@Api(tags = "单位职位管理")
@RestController
@RequestMapping("${api}")
public class CompanyPositionController extends BaseController {

    @Autowired
    private PositionService positionService;

    @Autowired
    private CollegeService collegeService;

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "分页查询职位", notes = "未审核状态升序 创建时间倒序")
    @OperLog(value = "职位管理", desc = "分页查询单位")
    @ApiPageParam
    @GetMapping("/company/position/page")
    public PageResult<PositionListVO> page(HttpServletRequest request) {
        PageParam<PositionListVO> pageParam = new PageParam<>(request);
        pageParam.put("userId", getLoginUserId());
        return new PageResult<>(positionService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "添加职位", desc = "添加职位信息", result = true)
    @ApiOperation(value = "添加职位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionFormVO", value = "positionFormVO", required = true, dataType = "PositionFormVO")
    })
    @PostMapping("/company/position/save")
    public JsonResult save(@Valid @RequestBody PositionFormVO positionFormVO) {
        positionFormVO.setCreateBy(getLoginUserId());
        return positionService.savePosition(positionFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "编辑职位", desc = "编辑职位信息", result = true)
    @ApiOperation(value = "编辑职位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionFormVO", value = "positionFormVO", required = true, dataType = "PositionFormVO")
    })
    @PutMapping("/company/position/update")
    public JsonResult update(@RequestBody PositionFormVO positionFormVO) {
        return positionService.updatePosition(positionFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "修改职位状态", desc = "修改职位状态", result = true)
    @ApiOperation(value = "修改职位状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionFormVO", value = "positionFormVO", required = true, dataType = "PositionFormVO")
    })
    @PutMapping("/company/position/update-state")
    public JsonResult updateState(@RequestBody PositionFormVO positionFormVO) {
        return positionService.updatePositionState(positionFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "获取单位详情")
    @OperLog(value = "用户单位管理", desc = "获取单位详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单位id", required = true, dataType = "int")
    })
    @GetMapping("/company/position/detail")
    public JsonResult detail(Integer id) {
        return positionService.detail(id);
    }

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "查询单位所有有效职位")
    @OperLog(value = "查询单位所有有效职位", desc = "查询单位所有有效职位")
    @GetMapping("/company/position/list")
    public JsonResult list() {
        return positionService.list(getLoginUserId());
    }


    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "获取职位专业")
    @OperLog(value = "获取职位专业", desc = "获取职位专业")
    @GetMapping("/company/position/majors")
    public JsonResult getMajors() {
        JsonResult result = collegeService.tree();
        List<College> list = (List<College>) result.getData();

//        College parent = new College();
//        parent.setId(-999);
//        parent.setCode("-999");
//        parent.setName(PushConstants.STUDENT_MAJOR_TYPE);
//
//        Major child = new Major();
//        child.setId(-1000);
//        child.setCode("-1000");
//        child.setName(PushConstants.STUDENT_MAJOR_TYPE);
//        List<Major> children = new ArrayList<>();
//        children.add(child);
//        parent.setChildren(children);
//
//        List<College> all = new ArrayList<>();
//        all.add(parent);
//        all.addAll(list);

        return JsonResult.ok().setData(list);
    }
}
